package com.huiminpay.merchant.controller;

import com.huiminpay.merchant.api.AppService;
import com.huiminpay.merchant.common.util.SecurityUtil;
import com.huiminpay.merchant.dto.AppDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: AppController
 * @Package: com.huiminpay.merchant.controller
 * @Description: 应用controller
 * @Author 谢同学
 * @Create 2023/11/20 19:21
 * @Version 1.0
 */
@Api(value = "商户平台-应用管理", tags = "商户平台-应用相关", description = "商户平台-应用相关")
@RestController
public class AppController {
    @Reference
    private AppService appService;

    /**
     * 创建商户应用
     * @param app
     * @return
     */
    @ApiOperation("商户创建应用")
    @ApiImplicitParams({@ApiImplicitParam(name = "app", value = "应用信息", required = true, dataType = "AppDTO", paramType = "body")})
    @RequestMapping(value = "/my/apps",method = {RequestMethod.POST,RequestMethod.PUT})
    public AppDTO createApp(@RequestBody AppDTO app) {
        //token中获取id
        Long merchantId = SecurityUtil.getMerchantId();
        return appService.createApp(merchantId,app);
    }

    /**
     * 根据商户id获取商户所有应用
     * @return
     */
    @ApiOperation("获取商户所有应用")
    @GetMapping("my/apps")
    public List<AppDTO> getAllApps() {
        //从token中获取商户id
        Long merchantId = SecurityUtil.getMerchantId();
       return appService.queryAppByMerchant(merchantId);
    }

    /**
     * 根据应用id查询应用详情
     * @param appId 应用id
     * @return
     */
    @ApiOperation("根据appid获取应用的详细信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "appId", value = "商户应用id", required = true, dataType = "String", paramType = "path")})
    @GetMapping("my/apps/{appId}")
    public AppDTO getApp(@PathVariable("appId") String appId) {
        return appService.getAppById(appId);
    }
}
